$(function () {
    //全局定义页数与总条数
    var markId = getUrlString('markid');
    var id = getUrlString('ershouid');
    var page = -1,totalSize = 0,userId2;
    function getnative(actionurl) {
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isiOS) {
            window.webkit.messageHandlers.getnative.postMessage(actionurl);
        }else {
            ikags.getnative(actionurl)
        }
    }
    function detail() {
        var data = {
            'markid':markId,
            'ershouid':id
        };
        getData('get','/niu/apiv3/ershou_getdetail.php',data,function (data) {
            console.log(data);
            $('#userface').attr('src',data.data.faceurl);
            $('#userName').html(data.data.nick);
            $('#price').html(data.data.price);
            if(data.data.uservip === '1'){
                $('#uservip').show()
            }else {
                $('#uservip').hide()
            }
            $('#communityUserPic,.communityUserName').click(function () {
                var id = data.data.userid;
                window.location.href = 'dancool://member='+id;
            });
            getData('post','/niu/apiv3/member_userinfo.php',{'markid':markId},function (data) {
                console.log(data);
                userId2 = data.data.id
            });

            //私聊
            $('#provateChat').click(function () {
                var userId = data.data.userid;
                var parameter = {
                    'markid':markId,
                    'blockuserid1':userId,
                    'blockuserid2':userId2
                };
                getData('post','/niu/apiv3/weibo_blackuser.php?method=itemcheckstate',parameter,function (data) {
                    console.log(data);
                    if (data.isblock === "0"){
                        let toUserName = 'djuser_'+userId;
                        getnative('dancool://message_enterSingleConversation='+toUserName+'');
                        window.location.href = 'dancool://fullweb='+urlView+'messagesV2/sessionList.html?toUserName='+toUserName;
                    }else {
                        laymsg('您两人处于黑名单状态下')
                    }
                });
            });

            $('#outtime').html(subTime(data.data.createtime));
            $('#intro').html(data.data.intro);
            $('#name').html(data.data.name);
            if(data.data.pics !== ''){
                var picUrls =  data.data.pics;
                var picUrlsArr = picUrls.split(';');
                var append = '';
                for (var i = 0;i < picUrlsArr.length;i++){
                    append += '<img src="'+picUrlsArr[i]+'" alt="">' ;
                }
                $('#infoImg').append(append);
                $('#infoImg img').click(function () {
                    var previewPic = $(this).attr('src');
                    window.location.href = 'dancool://web=https://api.odancool.com/weixinapp/view/preview/previewPic.html?previewPic='+previewPic;
                })
            }
            if(data.data.gzcount === '0'){
                $('.indexLike').hide()
            }else {
                $('#zancount').html(data.data.zancount);
                $('.indexLike').show()
            }
            if(data.data.plcount === '0'){
                $('#commentList').hide();
                $('.indexLike').css('margin-bottom','3rem');
            }else {
                $('.indexLike').css('margin-bottom','0rem');
                $('#commentList').show()
            }

            if(data.data.xyurl === ''){
                $('.noGoXY').removeClass('goXianYuBtn');
            }else {
                $('.noGoXY').addClass('goXianYuBtn');
            }
            $('.goXianYuBtn').click(function () {
                var url = data.data.xyurl;
                var xyUrl = url.replace(/&amp;/g,'&');
                window.location.href = 'dancool://sysweb='+xyUrl
            });
            //判断是否收藏
            if(data.data.iscollect === 0){
                $('#collect').attr('src','../../res/img/pullEgg/collect.png')
            }else{
                $('#collect').attr('src','../../res/img/pullEgg/isCollect.png')
            }

            // //判断是否是自己的帖子
            // if(data.data.ismine === 1){
            //     $('#delete').show();
            // }else {
            //     $("#delete").hide();
            // }
            // $('.zancountBtn').html(data.data.zancount);
            // $('.plcountBtn').html(data.data.plcount);
        })
    }
    detail();
    //点赞列表
    function zanCount () {
        var data = {
            'markid':markId,
            'type':'5',
            'postid':id
        };
        getData('get','/niu/apiv3/news_zanlistv2.php',data,function (data) {
            console.log(data);
            var append = '';
            $(data.data).each(function () {
                append += '<li class="toUserKu">' +
                    '           <input type="hidden" class="userId" value="'+this.userid+'">'+
                    '           <img src="'+this.userface+'" alt="">' +
                    '</li>'
            });
            $('#likeList').append(append);

            $('#likeList .toUserKu').click(function () {
                var userId = $(this).find('.userId').val();
                window.location.href = 'dancool://member='+userId;
            })
        })
    }
    zanCount();

    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isLock: true,
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#commentList').empty();
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                pinglunCount(page)
            }
        }
    });

    //评论列表
    function pinglunCount (page) {
        var data = {
            'markid':markId,
            'from':'5',
            'fromid':id,
            'page':page
        };
        getData('post','/niu/apiv3/news_getpinglunlist.php',data,function (data) {
            console.log(data);
            var append = '';
            $(data.data).each(function () {
                // var chenghaoStr = (this.userlv).substring(3);
                append += '<li>' +
                    '                <div class="communityUser">' +
                    '                    <input type="hidden" class="userId" value="'+this.userid+'">'+
                    '                    <div class="communityUserPic">' +
                    '                        <img src="'+this.userfaceurl+'" alt="">' +
                    '' +
                    '                    </div>' +
                    '                    <div class="communityUserName">' +
                    '                        <p>' +
                    '                            <span>'+this.usernick+'</span>' ;
                if(this.vip === '1'){
                    append +=  '              <img src="../../res/img/eggTV/monthCard@2x.png" alt="">' ;
                }
                // if (chenghaoStr < 6 && chenghaoStr > 0){
                //     append += '<img src="../../res/img/common/bronzeIcon.png" alt="">' ;
                // }else if (5 < chenghaoStr && chenghaoStr < 11){
                //     append += '<img src="../../res/img/common/silverIcon.png" alt="">' ;
                // }else if (10 < chenghaoStr && chenghaoStr < 16){
                //     append += '<img src="../../res/img/common/goldIcon.png" alt="">' ;
                // }else if (15 < chenghaoStr && chenghaoStr < 21){
                //     append += '<img src="../../res/img/common/platinaIcon.png" alt="">' ;
                // }else if(20 < chenghaoStr && chenghaoStr < 26){
                //     append += '<img src="../../res/img/common/diamondIcon.png" alt="">' ;
                // }
                append +='                </p>' +
                    '                        <p>'+subTime(this.time)+'</p>' +
                    '                    </div>' +
                    '                </div>' +
                    '                <div class="content1">' +
                    '                    <p class="info">'+this.intro+'</p>' +
                    '                </div>' +
                    '            </li>'
            });
            $('#commentList').append(append);
            totalSize = totalSize + 10;
            if (totalSize > data.datacount)
                mRefresh.endUpLoading(true);
            else{
                mRefresh.endUpLoading(false);
            }
            //
            $('#commentList li .communityUser').click(function () {
                var userId = $(this).find('.userId').val();
                window.location.href = 'dancool://member='+userId;
            })
        })
    }

    //点赞
    $('#likeBtn').click(function () {
        var params = {
            'markid':markId,
            'postid':id,
            'type':'5'
        };
        getData('get', '/niu/apiv3/news_dolikev2.php', params, function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                laymsg(data.msg);
                setTimeout(function () {
                    window.location.reload();
                },500)
            }else {
                laymsg(data.msg)
            }
        })
    });

    //收藏
    $('#collect').click(function () {
        var params = {
            'markid':markId,
            'postid':id,
            'type':'5'
        };
        getData('get', '/niu/apiv3/ershou_docollect.php', params, function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                laymsg(data.msg);
                if(data.command === 'add'){
                    $('#collect').attr('src','../../res/img/pullEgg/isCollect.png')
                }else {
                    $('#collect').attr('src','../../res/img/pullEgg/collect.png')
                }
            }else {
                laymsg(data.msg)
            }
        })
    });

    //评论
    $('.collect').click(function () {
        $(".guaXD").show().removeClass('bounceOutDown').addClass('bounceInUp');
    });

    var isAjax = true;
    $('#liuYan').click(function () {
        if(isAjax){
            var intro = $('#intro1').val();
            var params = {
                'markid': markId,
                'from': '5',
                'fromid': id,
                'fromid2':'0',
                'intro': intro,
                'title':'二手',
                'star':'5'
            };
            getData('post', '/niu/apiv3/news_dopinglun.php', params, function (data) {
                console.log(data);
                if (data.resultcode === '0') {
                    isAjax = false;
                    laymsg(data.msg);
                    setTimeout(function () {
                        window.location.reload();
                    }, 500)
                } else {
                    isAjax = true;
                    laymsg(data.msg)
                }
            })
        }

    });
    $('.qx').click(function () {
        $(".guaXD").removeClass('bounceInUp').addClass('bounceOutDown');
    });
});